package com.example.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 *
 * @author tong
 * @date 2023/03/13
 */
@Component
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object flag = request.getSession().getAttribute("flag");
        if (flag == null) {
            // 没有登录，回去登录
            response.sendRedirect("login.html");
            return false;
        }
        System.out.println("当前session:"+flag);
        if(!(boolean) flag){
            response.sendRedirect("login.html");
            return (boolean) flag;
        }
        return true;
    }
}
